#include "diskio.h" 	
#include "Flash.h"
#include "flash_trans.h"

 
#define EX_FLASH 0 

#define FLASH_SECTOR_SIZE 	BOOT_MSC_BLOCK_SIZE			  
	 			    

						
uint16_t	    FLASH_SECTOR_COUNT=SECTOR_COUNT;
#define FLASH_BLOCK_SIZE  	1 


DSTATUS disk_initialize (
	BYTE drv				/* Physical drive nmuber (0..) */
)
{	    
	if(drv)return RES_PARERR;
//	SPI_Flash_Init();
	FLASH_SECTOR_COUNT=SECTOR_COUNT;//W25Q64		 
	return RES_OK; 
}   

DSTATUS disk_status (
	BYTE drv		/* Physical drive nmuber (0..) */
)
{		
	if(drv)return RES_PARERR;   
  return RES_OK;
}


DRESULT disk_read (
	BYTE drv,		/* Physical drive nmuber (0..) */
	BYTE *buff,		/* Data buffer to store read data */
	DWORD sector,	/* Sector address (LBA) */
	BYTE count		/* Number of sectors to read (1..255) */
)
{
  if (!count)return RES_PARERR;//count????0,????????		
	if(drv)return RES_PARERR;	
	for(;count>0;count--)
	{
		FLASHin_ReadData(buff,sector*FLASH_SECTOR_SIZE,FLASH_SECTOR_SIZE);
		sector++;
		buff+=FLASH_SECTOR_SIZE;
	}
	return RES_OK;
}  


#if _READONLY == 0
DRESULT disk_write (
	BYTE drv,			/* Physical drive nmuber (0..) */
	const BYTE *buff,	        /* Data to be written */
	DWORD sector,		/* Sector address (LBA) */
	BYTE count			/* Number of sectors to write (1..255) */
)
{
  if (!count)return RES_PARERR;//count????0,????????		 	 
  if(drv)return RES_PARERR;
	for(;count>0;count--)
	{										    
		FLASHin_WriteData((uint8_t*)buff,sector*FLASH_SECTOR_SIZE,FLASH_SECTOR_SIZE);
		sector++;
		buff+=FLASH_SECTOR_SIZE;
	}
  return RES_OK;	 	 
}
#endif /* _READONLY */


DRESULT disk_ioctl (
	BYTE drv,		/* Physical drive nmuber (0..) */
	BYTE ctrl,		/* Control code */
	void *buff		/* Buffer to send/receive control data */
)
{	
 DRESULT res;
 if(drv)return RES_PARERR;		
	switch(ctrl)
	{
		case CTRL_SYNC:
		res = RES_OK; 
				break;	 
		case GET_SECTOR_SIZE:
				*(WORD*)buff = FLASH_SECTOR_SIZE;
				res = RES_OK;
				break;	 
		case GET_BLOCK_SIZE:
				*(WORD*)buff = FLASH_BLOCK_SIZE;
				res = RES_OK;
				break;	 
		case GET_SECTOR_COUNT:
				*(DWORD*)buff = FLASH_SECTOR_COUNT;
				res = RES_OK;
				break;
		default:
				res = RES_PARERR;
				break;
	}
  return res;
}   
                                                                                                                                                                                                                                           
DWORD get_fattime (void)
{				 
	return 0;
}			 


